local str = require "resty.string"
local headers = ngx.req.get_headers()
local terminal_control_tag_base = headers["Terminal-Control-Tag-Base"]
local terminal_control_tag_when = headers["Terminal-Control-Tag-When"]
local x_naja_detect = headers["X-NAJA-Detect"]
local valid = true
if not terminal_control_tag_base then
  valid = false
end
if not terminal_control_tag_when then
  valid = false
end
if not x_naja_detect then
  valid = false
end
if valid then
  local now = os.time()
  valid = (now*1000-terminal_control_tag_when) < 60000
  if valid then
    local resty_md5 = require "resty.md5"
    local md5 = resty_md5:new()
    md5:update(terminal_control_tag_base)
    md5:update("-")
    md5:update(terminal_control_tag_when)
    local digest = str.to_hex(md5:final())
    valid=digest==string.lower(x_naja_detect)
  end
end
if valid then
  return
end
local cjson = require "cjson"
local result = cjson.encode({
  code = 400,
  message = "非法请求"
})
ngx.say(result)